import 'package:factoryside/utils/localizations.dart';
import 'package:factoryside/utils/utils.dart';
import 'package:factoryside/widget/footer_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter/physics.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bugly/flutter_bugly.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:provider/provider.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'api/api.dart';
import 'home/page_login.dart';
import 'index.dart';
import 'provider/providers.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  bool isLogin;
  SharedPreferences.getInstance().then((value) async {
    Api.userName=value.getString('userName');
    Api.adminToken=value.getString('adminToken');
    isLogin=(Api.userName!=null&&Api.adminToken!=null);

    FlutterBugly.postCatchedException(() => runApp(MyApp(isLogin)));
  });
  //黑色
  SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(statusBarColor: Colors.transparent));
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  final bool isLogin;

  MyApp(this.isLogin);
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers:[//需要管理多个状态只需要在providers添加对应的状态
        ChangeNotifierProvider(create: (_)=>Bill()),
        ChangeNotifierProvider(create: (_)=>User()),
        ChangeNotifierProvider(create: (_)=>Head()),
      ],
      child: RefreshConfiguration(
          headerBuilder: () => MaterialClassicHeader(),        // 配置默认头部指示器,假如你每个页面的头部指示器都一样的话,你需要设置这个
          footerBuilder:  () => MyFooter(),        // 配置默认底部指示器
          headerTriggerDistance: 80.0,        // 头部触发刷新的越界距离
          springDescription:SpringDescription(stiffness: 170, damping: 16, mass: 1.9),         // 自定义回弹动画,三个属性值意义请查询flutter api
          maxOverScrollExtent :100, //头部最大可以拖动的范围,如果发生冲出视图范围区域,请设置这个属性
          maxUnderScrollExtent:0, // 底部最大可以拖动的范围
          enableScrollWhenRefreshCompleted: true, //这个属性不兼容PageView和TabBarView,如果你特别需要TabBarView左右滑动,你需要把它设置为true
          enableLoadingWhenFailed : true, //在加载失败的状态下,用户仍然可以通过手势上拉来触发加载更多
          hideFooterWhenNotFull: false, // Viewport不满一屏时,禁用上拉加载更多功能
          enableBallisticLoad: true, // 可以通过惯性滑动触发加载更多
          child: AnnotatedRegion<SystemUiOverlayStyle>(
            value: SystemUiOverlayStyle.dark,
            child: MaterialApp(
              localizationsDelegates: [
                ChineseCupertinoLocalizations.delegate,
//                      DefaultCupertinoLocalizations.delegate, // 目前只包含英文
                GlobalMaterialLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate,
              ],
              supportedLocales: [
                const Locale('zh', 'CN'),
                const Locale('en', 'US'),
              ],
              theme: ThemeData(
                  textTheme: TextTheme(
//                    headline6: TextStyle(fontSize: 16,color: Colors.black),
                    bodyText2: TextStyle(fontSize: 16),
                  ),
                  primarySwatch: Colors.red
              ),
              debugShowCheckedModeBanner: false,
              builder: (context, child) => Scaffold(
                // Global GestureDetector that will dismiss the keyboard
                body: GestureDetector(
                  onTap: () {
                    hideKeyboard(context);
                  },
                  child: child,
                ),
              ),
              home: isLogin?Index():Login(),
            ),
          )),
    );
  }

}
